package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class FileUploadController {
    /**
     * 文件上传
     */
    @PostMapping
    public Result upload(@RequestParam(value = "file") MultipartFile file) throws Exception {
        //封装文件信息
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),//文件名字，1.jpg
                file.getBytes(),//文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename())//获取文件扩展名
        );
        //调用FastDFSUtile工具类将文件传入Fast DFS中
        String[] uploads = FastDFSUtil.upload(fastDFSFile);
        //拼接访问地址
//        String url="http://192.168.0.102:8080/"+uploads[0]+"/"+uploads[1];
        String url=FastDFSUtil.getTrackerInfo()+"/"+uploads[0]+"/"+uploads[1];
        return new Result(true, StatusCode.OK, "上传成功",url);
    }
}
